package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.OverseasBean;
import com.sx.zongzhi.bean.OverseasDetailBean;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.presenter.OverseasDetailPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IOverseasDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 境外人员详情
 */
public class OverseasDetailActivity extends BaseActivity implements IOverseasDetailView{

    @BindView(R.id.imv_Head) ImageView imv_Head;
    @BindView(R.id.et_SurName) TextView tv_SurName;
    @BindView(R.id.et_Name) TextView tv_Name;
    @BindView(R.id.et_Sex) TextView tv_Sex;
    @BindView(R.id.et_Birthday) TextView tv_Birthday;
    @BindView(R.id.et_CredType) TextView tv_CredType;
    @BindView(R.id.et_IDCard) TextView tv_IDCard;
    @BindView(R.id.et_NowAddress) TextView tv_NowAddress;
    @BindView(R.id.tv_YesNoAddress) TextView tv_YesNoAddress;
    @BindView(R.id.tv_NowAddressTitle) TextView tv_NowAddressTitle;
    @BindView(R.id.tv_CurrentAddress) TextView tv_CurrentAddress;
    @BindView(R.id.et_Nationality) TextView tv_Nationality;
    @BindView(R.id.tv_Aims) TextView tv_Aims;
    @BindView(R.id.et_ContactWay) TextView tv_ContactWay;
    @BindView(R.id.et_ComeData) TextView tv_ComeData;
    @BindView(R.id.tv_BackData) TextView tv_BackData;
    @BindView(R.id.et_ValidData) TextView tv_ValidData;
    @BindView(R.id.et_ChainName) TextView tv_ChainName;
    @BindView(R.id.et_Follow) TextView tv_Follow;
    @BindView(R.id.et_WorkType) TextView tv_WorkType;
    @BindView(R.id.et_Profession) TextView tv_Profession;
    @BindView(R.id.et_Religion) TextView tv_Religion;
    @BindView(R.id.et_Service) TextView tv_Service;

    private String overseasId="";
    private OverseasDetailBean detailBean;
    private OverseasBean.PermissionBean permissionBean;
    private String orgId="";

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_overseas_detail);
        setTitleName("境外人口详情");
        ButterKnife.bind(this);
        permissionBean= (OverseasBean.PermissionBean) getIntent().getSerializableExtra("PermissionBean");
        orgId= getIntent().getStringExtra("orgId");
        overseasId=getIntent().getStringExtra("OverseasId");
        initView();
        initData();
    }

    private void initView() {
        if(permissionBean!=null){
            if (permissionBean.isDeleteOverseaPerson()){
                setRightImage(R.mipmap.icon_nav_delete);
            }
            if (permissionBean.isUpdateOverseaPerson()){
                setRightImage2(R.mipmap.icon_nav_edit);
            }
        }
    }

    private void initData() {
        LoadDialog.show(this);
        OverseasDetailPest detailPest=new OverseasDetailPest(this);
        detailPest.getOverseasDetail(overseasId);
    }


    @OnClick({R.id.imv_Right,R.id.imv_Right2})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.imv_Right:
                createDelete();
                break;
            case R.id.imv_Right2:
                if (detailBean!=null){
                    startActivity(new Intent(this,AddOverseasActivity.class)
                            .putExtra("OverseasDetailBean",detailBean)
                            .putExtra("orgId",orgId));
                    finish();
                }else {
                    ToastUtils.showToast(this,"获取详情失败");
                }
                break;

        }
    }

    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定删除此人员吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                OverseasDetailPest detailPest=new OverseasDetailPest(OverseasDetailActivity.this);
                detailPest.deleteOverseas(overseasId);
                LoadDialog.show(OverseasDetailActivity.this,"删除中...");
            }
        }).create();
        dialog.show();
    }

    @Override
    public void getOverSeasDetail(OverseasDetailBean detailBean) {
        this.detailBean=detailBean;
        LoadDialog.dismiss(this);
        showView();
    }

    @Override
    public void deleteOverseas(boolean success) {
        LoadDialog.dismiss(this);
        if (success){
            MyRxBus.get().post(new MsgEvent<>("OverseasActivity",2,"删除成功"));
            finish();
        }else {
            ToastUtils.showToast(this,"删除失败");
        }
    }

    private void showView() {
        tv_SurName.setText(detailBean.getForeignName());
        tv_Name.setText(detailBean.getEnglishName());
        tv_Sex.setText(detailBean.getGender()!=null?detailBean.getGender().getDisplayName():"未知");
        tv_Birthday.setText(detailBean.getBirthday());
        tv_CredType.setText(detailBean.getCertificateType()!=null?
            detailBean.getCertificateType().getDisplayName():"未知");
        tv_IDCard.setText(detailBean.getCertificateNo());
        tv_NowAddress.setText(detailBean.getAddress());
        if (detailBean.isIsHaveHouse()){
            tv_YesNoAddress.setText("有");
            tv_NowAddressTitle.setText("现住门(楼)详址");
            tv_CurrentAddress.setText(detailBean.getAddress());
        }else {
            tv_YesNoAddress.setText("无");
            tv_NowAddressTitle.setText("无住房原因");
            tv_CurrentAddress.setText(detailBean.getNoHouseReason());
        }
        tv_Nationality.setText(detailBean.getNationality()!=null?
            detailBean.getNationality().getDisplayName():"未知");
        tv_Aims.setText(detailBean.getInflowReason()!=null?
            detailBean.getInflowReason().getDisplayName():"未知");
        String contact= (BaseUtil.isEmpty(detailBean.getMobileNumber())?"":detailBean.getMobileNumber())
                +( BaseUtil.isEmpty(detailBean.getTelephone())?"":detailBean.getTelephone());
        tv_ContactWay.setText(contact);
        tv_ComeData.setText(detailBean.getArrivalTime());
        tv_BackData.setText(detailBean.getLeaveTime());
        tv_ValidData.setText(detailBean.getCertificateStrartDay()+" 至 "+detailBean.getCertificateEndDay());
        tv_ChainName.setText(detailBean.getName());
        if (detailBean.isIsEmphasis()){
            tv_Follow.setText("是");
        }else {
            tv_Follow.setText("否");
        }
        tv_WorkType.setText(detailBean.getProfession()!=null?
            detailBean.getProfession().getDisplayName():"未知");
        tv_Profession.setText(detailBean.getCareerText());
        tv_Religion.setText(detailBean.getFaith()!=null?
            detailBean.getFaith().getDisplayName():"未知");
        tv_Service.setText(detailBean.getWorkUnit());
        if (!BaseUtil.isEmpty(detailBean.getImgUrl())){
            final String path=ServiceFactory.NEW_API_BASE_URL+File.separator+detailBean.getImgUrl();
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.head_default)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(this)
                    .load(path)
                    .apply(options)
                    .into(imv_Head);
            imv_Head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(OverseasDetailActivity.this,ImagePlayActivity.class)
                    .putExtra("path",path));
                }
            });

        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }
}
